<?php
require 'config.php';
$user_id = $_REQUEST['user_id'];
$to_user_id = $_REQUEST['to_user_id'];
$order_id = $_REQUEST['order_id'];
$status = $_REQUEST['status'];
$time = date('Y-m-d H:i:s');
if ($status == 2 || $status == '2') {
    $sql = "update " . $oto . "_help_orders set orderStatus='2',overTime='{$time}' where id='{$order_id}'";
    $result = $db->query($sql);
    if ($result) {
        $data[0]['pan'] = 1;
        $data[0]['msg'] = "已完成";
    } else {
        $data[0]['pan'] = -1;
        $data[0]['msg'] = "操作失败";
    }
} else if ($status == 3 || $status == '3') {
    $sql = "select * from " . $oto . "_help_orders where id='{$order_id}'";
    $result = $db->query($sql);
    while ($row = $result->fetch_assoc()) {
        $order[] = $row; //将取得的所有数据赋值给person_info数组
    }
    if ($order[0]['overTime'] == null) {
        $sql = "update " . $oto . "_help_orders set orderStatus='3',overTime='{$time}' where id='{$order_id}'";
    } else {
        $sql = "update " . $oto . "_help_orders set orderStatus='3' where id='{$order_id}'";
    }

    $result = $db->query($sql);
    if ($result) {
        $sql = "select " . $oto . "_help_orders.*," . $oto . "_help.type from " . $oto . "_help_orders," . $oto . "_help where " . $oto . "_help_orders.id='{$order_id}' and " . $oto . "_help_orders.helpId=" . $oto . "_help.id";
        $result = $db->query($sql);
        while ($row = $result->fetch_assoc()) {
            $order_info[] = $row; //将取得的所有数据赋值给person_info数组
        }
        if (isset($order_info)) {
            $order_status = $order_info[0]['orderStatus'];
            if ($order_info[0]['overTime'] == 0 || !isset($order_info[0]['overTime'])) {
                $sql = "update " . $oto . "_help_orders set overTime='{$time}' where id='{$order_id}'";
                $result = $db->query($sql);
            }
            $for_user_id = 0;
            if ($order_info[0]['type'] == 0) {
                $for_user_id = $order_info[0]['acceptUserId'];
            }

            if ($order_info[0]['type'] == 1) {
                $for_user_id = $order_info[0]['userId'];
            }

            $user_hmoney = $order_info[0]['money'] - $order_info[0]['commission'];
            $sql = "update " . $oto . "_users set userMoney=userMoney+'$user_hmoney' where userId='{$for_user_id}'";
            $result = $db->query($sql);
            if ($result) {
                $time2 = time();
                $sql = "select userMoney,userScore from " . $oto . "_users where userId='{$for_user_id}'";
                $result = $db->query($sql);
                while ($row = $result->fetch_assoc()) {
                    $user_info[] = $row; //将取得的所有数据赋值给person_info数组
                }
                $user_money_lin = $user_info[0]['userMoney'];
                $sql = "INSERT INTO " . $oto . "_money_record (`userid`,`orderNo`,`money`,`balance`,`time`,`IncDec`,`type`,`remark`,`payWay`) VALUES ('$for_user_id','$order_id','$user_hmoney','$user_money_lin','$time2','1','1','微帮订单','3')";
                //file_put_contents("tsxx.txt", "\r\n".$sql."\r\n", FILE_APPEND);
                $result = $db->query($sql);
                $data[0]['pan'] = 1;
                $data[0]['msg'] = "确认成功";

                $sql = "select * from `" . $oto . "_help_orders` where `id`='" . $order_id . "' LIMIT 1";
                $result = $db->query($sql);
                while ($row = $result->fetch_assoc()) {
                    $order[] = $row; //将取得的所有数据赋值给person_info数组
                }
                $needPay_lin = $order[0]['money'];
                $user_score_lin = $user_info[0]['userScore'] + $needPay_lin;
                $sql = "update " . $oto . "_users set userScore=userScore+'$needPay_lin' where userId='{$user_id}'";
                $result = $db->query($sql);
                $sql = "INSERT INTO " . $oto . "_score_record (`userid`,`orderNo`,`score`,`totalscore`,`time`,`IncDec`,`type`) VALUES ('$user_id','$order_id','$needPay_lin','$user_score_lin','$time2','1','1')";
                $result = $db->query($sql);
            } else {
                $sql = "update " . $oto . "_help_orders set orderStatus='{$order_status}' where id='{$order_id}'";
                $result = $db->query($sql);
                $data[0]['pan'] = -1;
                $data[0]['msg'] = "确认失败";
            }
        } else {
            $data[0]['pan'] = -1;
            $data[0]['msg'] = "订单不存在";
        }
    } else {
        $data[0]['pan'] = -1;
        $data[0]['msg'] = "确认失败";
    }

} else if ($status == 'qx') {
    $sql = "select " . $oto . "_help_orders.*," . $oto . "_help.type from " . $oto . "_help_orders," . $oto . "_help where " . $oto . "_help_orders.id='{$order_id}' and " . $oto . "_help_orders.helpId=" . $oto . "_help.id";
    $result = $db->query($sql);
    while ($row = $result->fetch_assoc()) {
        $order_info[] = $row; 
        //将取得的所有数据赋值给person_info数组
    }
    if (isset($order_info)) {
        $order_status = $order_info[0]['orderStatus'];
        if ($order_status == 0 || $order_status == '0') {
            $sql = "update " . $oto . "_help_orders set orderStatus='-1' where id='{$order_id}'";
            $result = $db->query($sql);
            if ($result) {
                $data[0]['pan'] = 1;
                $data[0]['msg'] = "取消成功";
            } else {
                $data[0]['pan'] = -1;
                $data[0]['msg'] = "取消失败";
            }
        } else if ($order_status == 1 || $order_status == '1' || $order_status == 2 || $order_status == '2') {
            $for_user_id = 0;
            if ($order_info[0]['type'] == 0) {
                $for_user_id = $order_info[0]['userId'];
            }

            if ($order_info[0]['type'] == 1) {
                $for_user_id = $order_info[0]['acceptUserId'];
            }

            $user_hmoney = $order_info[0]['money'];
            $sql = "update " . $oto . "_users set userMoney=userMoney+'$user_hmoney' where userId='{$for_user_id}'";
            $result = $db->query($sql);
            if ($result) {
                //修改订单状态
                $sql = "update " . $oto . "_help_orders set orderStatus='-2',isRefund='2' where id='{$order_id}'";
                $result = $db->query($sql); 
                $time2 = time();
                $sql = "select userMoney from " . $oto . "_users where userId='{$for_user_id}'";
                $result = $db->query($sql);
                while ($row = $result->fetch_assoc()) {
                    $user_info[] = $row; 
                    //将取得的所有数据赋值给person_info数组
                }
                $user_money_lin = $user_info[0]['userMoney'];
                $sql = "INSERT INTO " . $oto . "_money_record (`userid`,`orderNo`,`money`,`balance`,`time`,`IncDec`,`type`,`remark`,`payWay`) VALUES ('$for_user_id','$order_id','$user_hmoney','$user_money_lin','$time2','1','2','微帮订单:服务方取消订单','3')";
                //file_put_contents("tsxx.txt", "\r\n".$sql."\r\n", FILE_APPEND);
                $result = $db->query($sql);
                $data[0]['pan'] = 1;
                $data[0]['msg'] = "取消成功";
            } else {
                $sql = "update " . $oto . "_help_orders set orderStatus='{$order_status}' where id='{$order_id}'";
                $result = $db->query($sql);
                $data[0]['pan'] = -1;
                $data[0]['msg'] = "取消失败";
            }
        }
        $helpId = $order_info[0]['helpId'];
        $sql = "update " . $oto . "_help_order_user set isDel='1' where help_id='{$helpId}' and (user_id='{$user_id}' or user_id='{$for_user_id}')";
        $result = $db->query($sql);
    } else {
        $data[0]['pan'] = -1;
        $data[0]['msg'] = "订单不存在";
    }
} else if ($status == 'sqtk') {
    $sql = "select " . $oto . "_help_orders.*," . $oto . "_help.type from " . $oto . "_help_orders," . $oto . "_help where " . $oto . "_help_orders.id='{$order_id}' and " . $oto . "_help_orders.helpId=" . $oto . "_help.id";
    $result = $db->query($sql);
    while ($row = $result->fetch_assoc()) {
        //将取得的所有数据赋值给person_info数组
        $order_info[] = $row; 
    }
    if (isset($order_info)) {
        $order_status = $order_info[0]['orderStatus'];
        if ($order_status == 1 || $order_status == '1') {
            $order_status2 = -3;
        } else if ($order_status == 2 || $order_status == '2') {
            $order_status2 = -4;
        }

        $sql = "update " . $oto . "_help_orders set orderStatus='{$order_status2}',isRefund='1',applyRefundTime='{$time}' where id='{$order_id}'";
        $result = $db->query($sql);
        if ($result) {
            $data[0]['pan'] = 1;
            $data[0]['msg'] = "申请成功";
        } else {
            $data[0]['pan'] = -1;
            $data[0]['msg'] = "申请失败";
        }
    } else {
        $data[0]['pan'] = -1;
        $data[0]['msg'] = "订单不存在";
    }
} else if ($status == 'qxtk') {
    $sql = "select " . $oto . "_help_orders.*," . $oto . "_help.type from " . $oto . "_help_orders," . $oto . "_help where " . $oto . "_help_orders.id='{$order_id}' and " . $oto . "_help_orders.helpId=" . $oto . "_help.id";
    $result = $db->query($sql);
    while ($row = $result->fetch_assoc()) {
        //将取得的所有数据赋值给person_info数组
        $order_info[] = $row; 
    }
    if (isset($order_info)) {
        $order_status = $order_info[0]['orderStatus'];
        if ($order_status == -3 || $order_status == '-3') {
            $order_status2 = 1;
        } else if ($order_status == -4 || $order_status == '-4') {
            $order_status2 = 2;
        } else if ($order_status == -7 || $order_status == '-7') {
            $order_status2 = 1;
        } else if ($order_status == -8 || $order_status == '-8') {
            $order_status2 = 2;
        } else {
            $order_status2 = $order_status;
        }

        $sql = "update " . $oto . "_help_orders set orderStatus='{$order_status2}',isRefund='0' where id='{$order_id}'";
        $result = $db->query($sql);
        if ($result) {
            $data[0]['pan'] = 1;
            $data[0]['msg'] = "取消成功";
        } else {
            $data[0]['pan'] = -1;
            $data[0]['msg'] = "取消失败";
        }
    } else {
        $data[0]['pan'] = -1;
        $data[0]['msg'] = "订单不存在";
    }
} else if ($status == 'jjtk') {
    $sql = "select " . $oto . "_help_orders.*," . $oto . "_help.type from " . $oto . "_help_orders," . $oto . "_help where " . $oto . "_help_orders.id='{$order_id}' and " . $oto . "_help_orders.helpId=" . $oto . "_help.id";
    $result = $db->query($sql);
    while ($row = $result->fetch_assoc()) {
        //将取得的所有数据赋值给person_info数组
        $order_info[] = $row; 
    }
    if (isset($order_info)) {
        $order_status = $order_info[0]['orderStatus'];
        if ($order_status == -3 || $order_status == '-3') {
            $order_status2 = 1;
        } else if ($order_status == -4 || $order_status == '-4') {
            $order_status2 = 2;
        }

        $sql = "update " . $oto . "_help_orders set orderStatus='{$order_status2}',isRefund='3' where id='{$order_id}'";
        $result = $db->query($sql);
        if ($result) {
            $data[0]['pan'] = 1;
            $data[0]['msg'] = "拒绝成功";
        } else {
            $data[0]['pan'] = -1;
            $data[0]['msg'] = "拒绝失败";
        }
    } else {
        $data[0]['pan'] = -1;
        $data[0]['msg'] = "订单不存在";
    }
} else if ($status == 'ptjr') {
    $sql = "select " . $oto . "_help_orders.*," . $oto . "_help.type from " . $oto . "_help_orders," . $oto . "_help where " . $oto . "_help_orders.id='{$order_id}' and " . $oto . "_help_orders.helpId=" . $oto . "_help.id";
    $result = $db->query($sql);
    while ($row = $result->fetch_assoc()) {
        //将取得的所有数据赋值给person_info数组
        $order_info[] = $row; 
    }
    if (isset($order_info)) {
        $order_status = $order_info[0]['orderStatus'];
        if ($order_status == 1 || $order_status == '1') {
            $order_status2 = -7;
        } else if ($order_status == 2 || $order_status == '2') {
            $order_status2 = -8;
        }

        $sql = "update " . $oto . "_help_orders set orderStatus='{$order_status2}',isRefund='-1' where id='{$order_id}'";
        $result = $db->query($sql);
        if ($result) {
            $data[0]['pan'] = 1;
            $data[0]['msg'] = "申请成功";
        } else {
            $data[0]['pan'] = -1;
            $data[0]['msg'] = "申请失败";
        }
    } else {
        $data[0]['pan'] = -1;
        $data[0]['msg'] = "订单不存在";
    }
} else if ($status == 'tytk') {
    $sql = "select " . $oto . "_help_orders.*," . $oto . "_help.type from " . $oto . "_help_orders," . $oto . "_help where " . $oto . "_help_orders.id='{$order_id}' and " . $oto . "_help_orders.helpId=" . $oto . "_help.id";
    $result = $db->query($sql);
    while ($row = $result->fetch_assoc()) {
        //将取得的所有数据赋值给person_info数组
        $order_info[] = $row; 
    }
    if (isset($order_info)) {
        $isPay = $order_info[0]['isPay'];
        $order_status = $order_info[0]['orderStatus'];
        $isRefund = $order_info[0]['isRefund'];
        if ($isPay == 1 || $isPay == '1') {
            $for_user_id = 0;
            if ($order_info[0]['type'] == 0) {
                $for_user_id = $order_info[0]['userId'];
            }

            if ($order_info[0]['type'] == 1) {
                $for_user_id = $order_info[0]['acceptUserId'];
            }

            $user_hmoney = $order_info[0]['money'];
            $sql = "update " . $oto . "_users set userMoney=userMoney+'$user_hmoney' where userId='{$for_user_id}'";
            $result = $db->query($sql);
            if ($result) {
                $sql = "update " . $oto . "_help_orders set orderStatus='-5',isRefund='2',refundTime='{$time}' where id='{$order_id}'";
                $result = $db->query($sql);
                $time2 = time();
                $sql = "select userMoney from " . $oto . "_users where userId='{$for_user_id}'";
                $result = $db->query($sql);
                while ($row = $result->fetch_assoc()) {
                     //将取得的所有数据赋值给person_info数组
                    $user_info[] = $row;
                }
                $user_money_lin = $user_info[0]['userMoney'];
                $sql = "INSERT INTO " . $oto . "_money_record (`userid`,`orderNo`,`money`,`balance`,`time`,`IncDec`,`type`,`remark`,`payWay`) VALUES ('$for_user_id','$order_id','$user_hmoney','$user_money_lin','$time2','1','2','微帮订单：退款','3')";
                //file_put_contents("tsxx.txt", "\r\n".$sql."\r\n", FILE_APPEND);
                $result = $db->query($sql);
                $data[0]['pan'] = 1;
                $data[0]['msg'] = "退款成功";
                $helpId = $order_info[0]['helpId'];
                $sql = "update " . $oto . "_help_order_user set isDel='1' where help_id='{$helpId}' and user_id='{$user_id}'";
                $result = $db->query($sql);
            } else {
                $sql = "update " . $oto . "_help_orders set orderStatus='{$order_status}',isRefund='{$isRefund}' where id='{$order_id}'";
                $result = $db->query($sql);
                $data[0]['pan'] = -1;
                $data[0]['msg'] = "退款失败";
            }
        } else {
            $data[0]['pan'] = -1;
            $data[0]['msg'] = "对方还未付款";
        }

    } else {
        $data[0]['pan'] = -1;
        $data[0]['msg'] = "订单不存在";
    }
}
echo json_encode($data);
